/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.imenu.web;

import com.imenu.ejb.ImenuStoreFacade;
import com.imenu.ejb.ImenuUserFacade;
import com.imenu.entity.ImenuStore;
import com.imenu.entity.ImenuUser;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author yunzi
 */
@WebServlet(name = "StoreServlet", urlPatterns = {"/store"})
public class StoreServlet extends BaseServlet<ImenuStore> {
    
    @EJB
    ImenuStoreFacade storeFacade;
    @EJB
    ImenuUserFacade userFacade;
    void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idParam = request.getParameter("id");
        ImenuStore store = null;
        if (idParam == null || idParam.length() < 1) {
            store = new ImenuStore();
            this.parseRequest(request, store);
            storeFacade.create(store);
            ImenuUser user = (ImenuUser) request.getSession(true).getAttribute("userInSession");
            user.setStore(store);
            userFacade.edit(user);
            forward(request, response, "/menu?command=menuInfo");
        } else {
            store = storeFacade.find(Integer.valueOf(idParam));
            this.parseRequest(request, store);
            storeFacade.edit(store);
            ImenuUser user = (ImenuUser) request.getSession(true).getAttribute("userInSession");
            user.setStore(store);
            forward(request, response, "/menu?command=menuList");
        }
    }
    
    void storeInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ImenuUser user = (ImenuUser) request.getSession(true).getAttribute("userInSession");
        request.setAttribute("store", user.getStore());
        forward(request, response, "/store/storeInfo.jsp");
    }
    
}
